home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / 32SNIPIT.PAK / OPTDBPAR.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  96 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // optdbpar.c
  4. #include "snipit.h"
  5.  
  6. //=====================================================================
  7. //  Function:
  8. //          OptionalDBParam();
  9. //
  10. //  Description:
  11. //          This function shows how to use optional parameters
  12. //          with the DbiOpenDatabase function to set the USER NAME.
  13. //=====================================================================
  14. void
  15. OptionalDBParam (void)
  16. {
  17.     DBIResult   rslt;               // Return value from BDE Functions
  18.     hDBIDb      hDb = 0;            // Handle to the database
  19.     pCHAR       Alias="IBLOCAL";    // Alias Name
  20.     pCHAR       Driver="INTRBASE";  // Driver Type
  21.     pCHAR       Password="TEST";    // Password
  22.     pCHAR       UserName="test";    // User Name
  23.     pFLDDesc    pfldDesc;           // Field descriptor for optional
  24.                                     //   parameters
  25.     UINT16      iFields;            // Number of fields (optional
  26.                                     //   parameters)
  27.     CHAR        szTemp[100];        // Maximum length of the path
  28.                                     //   in the configuration file
  29.     pCHAR       szData;             // Data to display
  30.  
  31.     Screen("*** Using Optional Parameters with DbiOpenDatabase ***\r\n");
  32.  
  33.     rslt = DbiInit(NULL);
  34.     if (ChkRslt(rslt, "Init") == DBIERR_NONE)
  35.     {
  36.         szData = (pCHAR) malloc(DBIMAXSCRECSIZE);
  37.         pfldDesc = (pFLDDesc) malloc(DBIMAXSCFIELDS * sizeof(FLDDesc));
  38.         if ((szData == NULL) || (pfldDesc == NULL))
  39.         {
  40.             Screen("        Error - Out of Memory!");
  41.             if (szData) free(szData);
  42.             if (pfldDesc) free(pfldDesc);
  43.             DbiExit();
  44.             Screen("\r\n*** End of Example ***");
  45.             return;
  46.         }
  47.  
  48.         // Initialize the pfldDesc variable to all zeroes.
  49.         memset((void*) pfldDesc , 0, sizeof(FLDDesc) * DBIMAXSCFIELDS);
  50.  
  51.         iFields = 1;
  52.  
  53.         // Set the user name
  54.         sprintf(szTemp, "%s", UserName);
  55.         strcpy(&szData[0], szTemp);
  56.  
  57.         // Set up the parameter descriptor
  58.         pfldDesc[0].iFldNum = 1;
  59.         pfldDesc[0].iLen = (UINT16)(strlen(&szData[0]) + 1);
  60.         pfldDesc[0].iUnits1 = DBIMAXSCFLDLEN - 1;
  61.         strcpy(pfldDesc[0].szName, "USER NAME");
  62.  
  63.         // Open the database
  64.         rslt = DbiOpenDatabase(Alias, Driver, dbiREADWRITE,
  65.                                dbiOPENSHARED, Password, iFields,
  66.                                pfldDesc, (pBYTE) szData, &hDb);
  67.  
  68.         // Get a handle to an IDAPI database (STANDARD database handle
  69.         //   can be returned, but this is mainly used for SQL connections).
  70.         if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE)
  71.         {
  72.             Screen("        Error - Need to have an 'IBTEST' alias defined,");
  73.             Screen(" as well as having the user name\r\n");
  74.             Screen("           'test' defined with a password of 'TEST'...");
  75.             rslt = DbiExit();
  76.             Screen("\r\n*** End of Example ***");
  77.             ChkRslt(rslt, "Exit");
  78.             return;
  79.         }
  80.         else
  81.         {
  82.             Screen("\r\n    Connection established... ***");
  83.         }
  84.     }
  85.  
  86.  
  87.     Screen("\r\n*** End of Example ***");
  88.     rslt = DbiCloseDatabase(&hDb);
  89.     ChkRslt(rslt, "CloseDatabase");
  90.     DbiExit();
  91.  
  92.     return;
  93. }
  94.  
  95.  
  96.